//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    text:""
  },
  onShow: function () {
    var that = this
    //调用应用实例的方法获取全局数据
    that.setData({
        text: new Date()
      })
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    console.log('onLoad')
    // wx.connectSocket({
    //   url: 'ws://localhost:8080/websocket/' + 'burt'
    // })
    // wx.onSocketOpen(function (res) {
    //   console.log('WebSocket连接已打开！')
    // })
    // wx.onSocketError(function (res) {
    //   console.log('WebSocket连接打开失败，请检查！')
    // })
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
  },
  //事件处理函数
  requestTest: function () {
    wx.request({
     // url: 'http://m.zpcx.cn/product/306',
      url: 'http://baidu.com',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
      }
    })
  },
  webSocketTest: function () {
    wx.connectSocket({
     // url: 'ws://localhost:8080/websocket/burt'
      url: 'ws://localhost:8080/websocket'
    })
    wx.onSocketOpen(function (res) {
      console.log('WebSocket连接已打开！')
    })
    wx.onSocketError(function (res) {
      console.log('WebSocket连接打开失败，请检查！')
    })
    wx.onSocketMessage(function (res) {
      console.log('收到服务器内容：' + res.data)
    })
  },
  webSocketClose: function () {
    wx.connectSocket({
      url: 'ws://localhost:8080/websocket/burt'
    })
    //注意这里有时序问题，
    //如果 wx.connectSocket 还没回调 wx.onSocketOpen，而先调用 wx.closeSocket，那么就做不到关闭 WebSocket 的目的。
    //必须在 WebSocket 打开期间调用 wx.closeSocket 才能关闭。
    wx.onSocketOpen(function () {
      wx.closeSocket()
    })

    wx.onSocketClose(function (res) {
      console.log('WebSocket 已关闭！')
    })
  },
  sendMessageWS_burt: function () {
    var userId = 'burt';
    console.log('sendMessageWS_burt')
    // wx.connectSocket({
    //   url: 'ws://localhost:8080/websocket/' + userId
    // })
    var socketOpen = true
    var socketMsgQueue = [10086, 4399]
    //  wx.onSocketOpen(function (res) {
    socketOpen = true
    for (var i = 0; i < socketMsgQueue.length; i++) {
      sendSocketMessage(socketMsgQueue[i])
    }
    socketMsgQueue = []
    //})
    wx.onSocketMessage(function (res) {
      console.log(userId + ' 收到服务器内容：' + res.data)
    })

    wx.onSocketClose(function (res) {
      console.log('WebSocket 已关闭！')
    })

    function sendSocketMessage(msg) {
      if (socketOpen) {
        wx.sendSocketMessage({
          data: msg
        })
      } else {
        socketMsgQueue.push(msg)
      }
    }
  },

  sendMessageWS_rose: function () {
    var userId = 'reose';
    console.log('sendMessageWS_rose')
    wx.connectSocket({
      url: 'ws://localhost:8080/websocket/' + userId
    })
    var socketOpen = false
    var socketMsgQueue = [10010, '7k7k']
    wx.onSocketOpen(function (res) {
      socketOpen = true
      for (var i = 0; i < socketMsgQueue.length; i++) {
        sendSocketMessage(socketMsgQueue[i])
      }
      socketMsgQueue = []
    })
    wx.onSocketMessage(function (res) {
      console.log(userId + ' 收到服务器内容：' + res.data)
    })

    function sendSocketMessage(msg) {
      if (socketOpen) {
        wx.sendSocketMessage({
          data: msg
        })
      } else {
        socketMsgQueue.push(msg)
      }
    }
  }


})
